package 基础语法.常用API;

import java.util.Objects;

public class Obejct_Objects implements Cloneable {// 实现这个接口，当前对象才可以被克隆
    int a;
    int b;
    private int c;
    protected int d;
    String s;
    boolean deepClone = false;
    int[] array = new int[2];

    public static void main(String[] args) throws CloneNotSupportedException {
        Object Object = new Object();
        System.out.println(Object.toString() + "_" + new java.lang.Object() {
            @Override
            public String toString() {
                return "哈嘎嘎嘎";
            }
        });

        Object o1 = new Object() {
            @Override
            public boolean equals(java.lang.Object obj) {
                if (obj.toString() == this.toString()) {
                    return true;
                }
                return false;
            }

            @Override
            public String toString() {
                return "哈嘎嘎嘎";
            }
        };

        Object o2 = new Object() {
            @Override
            public String toString() {
                return "不哈嘎嘎嘎";
            }

            @Override
            public boolean equals(java.lang.Object obj) {
                return true;
            }

        };
        System.out.println(o1.equals(o2));// 默认equals是比较地址，this==obj
        System.out.println(o2.equals(o1));
        System.out.println(Objects.equals(o1, o2));
        System.out.println(Objects.equals(o2, o1));//????
         System.out.println(Objects.equals(o2, null));//自带非空判断
          System.out.println(Objects.isNull(null));
         System.out.println(Objects.nonNull(null));
          
          
        

        Obejct_Objects objects1 = new Obejct_Objects();
        objects1.a = 1;
        objects1.b = 2;
        objects1.c = 3;
        objects1.d = 4;
        objects1.array = new int[] { 1, 2 };
        objects1.s = "objects1";
        Obejct_Objects objects2 = (Obejct_Objects) objects1.clone();
        System.out.println(objects2);
        objects2.array[0] = 333;// 浅克隆，objects1也会被修改
        objects2.s = "objects2";
        System.out.println(objects1 + "\n" + objects2 + "\n");

        // 深克隆，手动实现
        objects1.deepClone = true;
        Obejct_Objects objects3 = (Obejct_Objects) objects1.clone();
        System.out.println(objects3);
        objects3.array[0] = 4444;// 浅克隆，objects1也会被修改
        objects3.s = "objects3";
        System.out.println(objects1 + "\n" + objects3);

    }

    @Override
    public String toString() {

        return a + "_" + b + "_" + c + "_" + d + "_[" + array[0] + "," + array[1] + "]" + "_" + s;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        if (this.deepClone) {
            int[] newArray = new int[this.array.length];
            for (int i = 0; i < newArray.length; i++) {
                newArray[i] = this.array[i];
            }
            Obejct_Objects objects = (Obejct_Objects) super.clone();
            objects.array = newArray;

            return objects;
        }
        return super.clone();

    }
}
